package com.atguigu.spring5.jdbctemplate.service;

import com.atguigu.spring5.jdbctemplate.dao.AccountDao;
import com.sun.xml.internal.fastinfoset.util.ValueArrayResourceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
//@Transactional(propagation = Propagation.REQUIRED) // 传播行为
//@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.REPEATABLE_READ) // 隔离级别
@Transactional(timeout = -1,propagation = Propagation.REQUIRED,isolation = Isolation.REPEATABLE_READ) // 超时时间
//@Transactional(noRollbackFor = {IndexOutOfBoundsException.class, ValueArrayResourceException.class},rollbackFor = {NullPointerException.class,NoSuchFieldException.class},readOnly = true,timeout = -1,propagation = Propagation.REQUIRED,isolation = Isolation.REPEATABLE_READ) // readOnly：是否只读、roolbackFor：回滚、noRoolbackFor：不回滚
public class AccountService {

    @Autowired
    private AccountDao accountDao;
    // 转账
    public void transferMoney(){
        // ------ 声明式事务管理
        accountDao.reduceMoney();
        // 模拟异常
        int i = 10/0;
        accountDao.addMoney();


        // ------ 编程式事务管理
//        try {
//            // 1 开启事务
//
//            // 2 业务操作
//            accountDao.reduceMoney();
//            // 模拟异常
//            int i = 10/0;
//            accountDao.addMoney();
//
//            // 3 业务没异常，提交事务
//
//        }catch (Exception e){
//            // 4 出现异常，事务回滚
//
//        }

    }
}
